package cs4280.ass2.admin.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;

import cs4280.ass2.entity.House;
import cs4280.ass2.entity.HouseSeat;
import cs4280.ass2.entity.Movie;
import cs4280.ass2.entity.Schedule;
import cs4280.ass2.entity.ScheduleSeatDetail;
import cs4280.ass2.persistence.HouseDao;
import cs4280.ass2.persistence.MovieDao;
import cs4280.ass2.persistence.ScheduleDao;
import cs4280.ass2.persistence.ScheduleSeatDetailDao;
import cs4280.ass2.util.FormatConstant;

public class ScheduleListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type = req.getParameter("type");
		String sid = req.getParameter("sid");
		
		String movieIdStr = req.getParameter("mid");

		if(StringUtils.equals(type, "delete") && NumberUtils.isDigits(sid)){
			List<ScheduleSeatDetail> scheduleList = ScheduleSeatDetailDao.getInstance().retrieveSeatListBySchedule(Long.valueOf(sid));
			if(scheduleList.size()==0){
				ScheduleDao.getInstance().deleteSchedule(Long.valueOf(sid));
			}else{
				req.setAttribute("message.info", "message.info.schedule.cannot.delete");
				prepareData(req, NumberUtils.toLong(movieIdStr));
				getServletContext().getRequestDispatcher("/admin/movie/schedule.jsp").forward(req, resp);
				return;
			}
		}
		
		if (NumberUtils.isDigits(movieIdStr)) {
			prepareData(req, NumberUtils.toLong(movieIdStr));
			getServletContext().getRequestDispatcher("/admin/movie/schedule.jsp").forward(req, resp);
		} else {
			resp.sendRedirect("list.do");
		}
	}

	private void prepareData(HttpServletRequest req, long movieId) {
		Movie movie = MovieDao.getInstance().retrieveMovie(movieId);
		List<Schedule> scheduleList = ScheduleDao.getInstance().retrieveScheduleByMovie(movieId);
		List<House> houseList = HouseDao.getInstance().retrieveAllHouse();

		req.setAttribute("movie", movie);
		req.setAttribute("scheduleList", scheduleList);
		req.setAttribute("houseList", houseList);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		long movieId = NumberUtils.toLong(req.getParameter("movieId"));
		long houseId = NumberUtils.toLong(req.getParameter("houseId"));
		double price = NumberUtils.toDouble(req.getParameter("price"));
		String dateStr = req.getParameter("scheduleStartDate");
		String timeStr = req.getParameter("scheduleStartTime");
		int movieDuration = NumberUtils.toInt(req.getParameter("movieDuration"));

		Date scheduleStart = null;
		try {
			scheduleStart = DateUtils.parseDate(dateStr + " " + timeStr, new String[] { FormatConstant.SCHEDULE_DATE_FORMAT });

			Schedule schedule = new Schedule();
			schedule.setCreateDate(new Date());
			schedule.setCreateUser(0);
			schedule.setModifyDate(new Date());
			schedule.setModifyUser(0);
			schedule.setMovieId(movieId);
			schedule.setHouseId(houseId);
			schedule.setPrice(price);
			schedule.setScheduleStart(scheduleStart);
			schedule.setScheduleEnd(DateUtils.addMinutes(scheduleStart, movieDuration));

			schedule = ScheduleDao.getInstance().addSchedule(schedule);
			House house = HouseDao.getInstance().retrieveHouse(houseId);
			for (HouseSeat seat : house.getSeatLocationList()) {
				ScheduleSeatDetailDao.getInstance().addScheduleSeatDetail(
						new ScheduleSeatDetail(schedule.getId(), seat.getRow(),seat.getCol(), ScheduleSeatDetail.SEAT_STATUS_AVAILABLE),1);
			}

			req.setAttribute("message.info", "message.info.schedule.addsuccess");
			prepareData(req, movieId);
			getServletContext().getRequestDispatcher("/admin/movie/schedule.jsp").forward(req, resp);
		} catch (ParseException e) {
			req.setAttribute("message.error", e.getMessage());
			getServletContext().getRequestDispatcher("/admin/movie/schedule.jsp").forward(req, resp);
		}
	}
}
